package org.cwnuit.course.action;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.course.util.ReflectionUtils;
import org.cwnuit.course.service.ClassesService;
import org.cwnuit.course.service.ClassroomService;
import org.cwnuit.course.domain.Classes;
import org.cwnuit.course.domain.Classroom;

/**
 * 班级管理Controller
 * @author john
 */
@SuppressWarnings("serial")
public class ClassesController extends HttpServlet {
	
	ClassesService classesService = new ClassesService();
	ClassroomService classroomService = new ClassroomService();
	/**
	 * 进入管理员查看班级列表
	 */
	public void classeslist(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		List<Classes> pm =classesService.listClasses();
		request.setAttribute("pm", pm);
		linkMsgRequest("班级列表","box-info", "你可以对班级进行添加、修改、删除等操作！" , "web/classes/classeslist.jsp" , request, response);
	}
	
	/**
	 * 进入管理员添加新班级和修改的界面
	 */
	public void classesInput(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int classid = Integer.parseInt(request.getParameter("classid"));
		List<Classroom> listRoom = classroomService.listClassroom();
		request.setAttribute("listRoom", listRoom);
		if (classid == 0) {
			linkMsgRequest("添加新班级","box-info", "请认真填写班级信息！" , "web/classes/classesinput.jsp" , request, response);
		} else {
			Classes classes = classesService.getClasses(classid);
			request.setAttribute("classes", classes);
			linkMsgRequest("修改班级信息","box-info", "请认真修改班级信息！" , "web/classes/classesinput.jsp" , request, response);
		}
	}

	/**
	 * 新班级和修改班级的提交处理
	 */
	public void classesSubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String classidStr = request.getParameter("classid")==""?"0":request.getParameter("classid");
		int classid = Integer.parseInt(classidStr);
		if(classesService.saveClasses(request)){
			if (classid == 0) {
				linkMsgRequest("添加新班级","box-info", "恭喜你，你已经成功添加班级信息！" , "web/classes/classesinput.jsp" , request, response);
			} else {
				List<Classes> pm = classesService.listClasses();
				request.setAttribute("pm", pm);
				linkMsgRequest("班级列表","box-info", "恭喜你，成功修改班级信息！" , "web/classes/classeslist.jsp" , request, response);
			}
		}else{
			List<Classes> pm = classesService.listClasses();
			request.setAttribute("pm", pm);
			linkMsg("班级列表","box-error", "对不起你的操作未能成功！" , "web/classes/classeslist.jsp" , request, response);
		}
	}
	
	/**
	 * 删除用户信息
	 */
	public void deleteClasses(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("classid")==null?"0":request.getParameter("classid");
		int classid = Integer.parseInt(cid);
		
		boolean deleteBoolean = classesService.deleteClasses(classid);
		List<Classes> pm = classesService.listClasses();
		request.setAttribute("pm", pm);
		
		if (deleteBoolean) {
			linkMsgRequest("班级列表","box-info", "恭喜你，你已经成功删除一条班级信息！" , "web/classes/classeslist.jsp" , request, response);
		} else {
			linkMsgRequest("班级列表","box-error", "对不起，删除班级信息失败！" , "web/classes/classeslist.jsp" , request, response);
		}
	}
	/**
	 **跳转与提示信息 公共方法
	 **信息级别的描述-msgrank
	 *级别0-普通框 值空
	 *级别1-普通信息框 值box-info
	 *级别2-普通警告框 值box-warning
	 *级别3-普通错误框 值box-error
	 *级别4-错误信息列表框 值box-error-msg
	 *参数：标题 信息的样式 提示信息 跳转地址
	 */
	public void linkMsgRequest(String tittle, String msgrank, String msg, String linkStr, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getSession().setAttribute("content_tittle",tittle);
		request.getSession().setAttribute("msgrank",msgrank);
		request.getSession().setAttribute("msg",msg);
		request.getRequestDispatcher(linkStr).forward(request,response);
	}
	//直接跳转
	public void linkMsg(String tittle, String msgrank, String msg, String linkStr, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getSession().setAttribute("content_tittle",tittle);
		request.getSession().setAttribute("msgrank",msgrank);
		request.getSession().setAttribute("msg",msg);
		response.sendRedirect(linkStr);
	}
	
	/**
	 * 用户数据Controller过滤器
	 * cmd即为servlet中的方法名
	 * 公共方法，勿动！
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		Method method = ReflectionUtils.getDeclaredMethod(ClassesController.class, cmd);
		try {
			if(method!=null){
				method.invoke(new ClassesController(),request, response);
			}else{
				request.getRequestDispatcher("login.jsp").forward(request,response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
